using BuildingBlocks.Infrastructure.EventBus;
using Identity.Application.Contracts.Common;
using Identity.Application.Contracts.Events;
using Identity.Application.Contracts.Services;
using Identity.Application.Users.Commands;
using MediatR;

namespace Identity.Application.Users.CommandHandler
{
    public class SendConfrimEmailCommandHandler : IRequestHandler<SendConfrimEmailCommand, Result>
    {
        private readonly IUserService _userService;
        private readonly IEventBus _eventBus;

        public SendConfrimEmailCommandHandler(IUserService userService, IEventBus eventBus)
        {
            _userService = userService;
            _eventBus = eventBus;
        }
        public async Task<Result> Handle(SendConfrimEmailCommand request, CancellationToken cancellationToken)
        {
            var user = await _userService.GetUserByIdAsync(request.userId);
            if (user == null) return Result.Failure("用户不存在");

            //推送事件
            var userRegisteredEvent = new UserRegisteredEvent(user.Id, user.Email);

            await _eventBus.PublishAsync(userRegisteredEvent);

            return Result.Success(new { message="已经发送邮件"});
        }
    }
}